import {RouteRecordRaw} from "vue-router"
import {ElMessage} from "element-plus"

/**
 * 处理文件名称
 */
const defaultGetName = (modelFileName: any) => {
    let registerModelName = modelFileName

    try {
        const matchNameResult = modelFileName.match(/.\/(.+?)\./)
        if (matchNameResult) {
            registerModelName = matchNameResult[1]
        } else {
            registerModelName = modelFileName.split('./')[1].split('.')[0]
        }
    } catch (e) {
        console.log("菜单未加载成功，错误信息：" + e)
        ElMessage.error("菜单未加载成功，请联系管理员")
    }
    return registerModelName
}

/**
 * 拼接导出的文件成数组
 */
const importMetaGlobDefaultContentTransfer = (modules: Record<string, any>, getName = defaultGetName) => {
    return Object.keys(modules).reduce((memo, moduleFileName) => {
        const moduleContent: any = modules[moduleFileName].default
        if (moduleContent) {
            const registerModuleName = getName(moduleFileName)
            memo[registerModuleName] = moduleContent
        }
        return memo
    }, {} as Record<string, any>)
}

/**
 * 导入模块并生成路由数组
 */
const importRoutes = import.meta.glob('./***/*.ts', {
    eager: true
})
const routes = Object.values(importMetaGlobDefaultContentTransfer(importRoutes)).flatMap(item => item)

/**
 * 路由
 */
const routerList: RouteRecordRaw[] = [
    /**
     * 驾驶舱
     */
    {
        path: "/cockpit",
        name: "cockpit",
        component: () => import("@/pages/cockpit/index.vue"),
        meta: {
            title: "驾驶舱",
            icon: "Edit"
        }
    },
    /**
     * 系统
     */
    //基础信息
    {
        path: "/code-rule",
        name: "code-rule",
        component: () => import("@/pages/system/code-rule/index.vue"),
        meta: {
            title: "编码管理"
        }
    },
    {
        path: "/menu-setting",
        name: "menu-setting",
        component: () => import("@/pages/system/menu-setting/index.vue"),
        meta: {
            title: "菜单管理"
        }
    },
    /**
     * 基础
     */
    {
        path: "/department",
        name: "department",
        component: () => import("@/pages/base-info/department/index.vue"),
        meta: {
            title: "部门管理"
        }
    },
    {
        path: "/staff",
        name: "staff",
        component: () => import("@/pages/base-info/staff/index.vue"),
        meta: {
            title: "员工管理"
        }
    },
    {
        path: "/customer",
        name: "customer",
        component: () => import("@/pages/base-info/customer/index.vue"),
        meta: {
            title: "客户管理"
        }
    },
    {
        path: "/goods",
        name: "goods",
        component: () => import("@/pages/base-info/goods/index.vue"),
        meta: {
            title: "物料管理"
        }
    },
    {
        path: "/supplier",
        name: "supplier",
        component: () => import("@/pages/base-info/supplier/index.vue"),
        meta: {
            title: "供应商管理"
        }
    },
    {
        path: "/logistics-company",
        name: "logistics-company",
        component: () => import("@/pages/base-info/logistics-company/index.vue"),
        meta: {
            title: "物流公司"
        }
    },
    {
        path: "/warehouse",
        name: "warehouse",
        component: () => import("@/pages/base-info/warehouse/index.vue"),
        meta: {
            title: "仓库管理"
        }
    },
    {
        path: "/warehouse-position",
        name: "warehouse-position",
        component: () => import("@/pages/base-info/warehouse-position/index.vue"),
        meta: {
            title: "仓位管理"
        }
    },
    /**
     * 仓库
     */
    {
        path: "/warehouse-real",
        name: "warehouse-real",
        component: () => import("@/pages/wms/warehouse-real/index.vue"),
        meta: {
            title: "实时库存"
        }
    },
    {
        path: "/warehouse-balance",
        name: "warehouse-balance",
        component: () => import("@/pages/wms/warehouse-balance/index.vue"),
        meta: {
            title: "库存结存"
        }
    },
    {
        path: "/warehouse-inventory",
        name: "warehouse-inventory",
        component: () => import("@/pages/wms/warehouse-inventory/index.vue"),
        meta: {
            title: "仓库库存"
        }
    },
    {
        path: "/warehouse-otherIn",
        name: "warehouse-otherIn",
        component: () => import("@/pages/wms/warehouse-otherIn/index.vue"),
        meta: {
            title: "其他入库"
        }
    },
    {
        path: "/warehouse-otherOut",
        name: "warehouse-otherOut",
        component: () => import("@/pages/wms/warehouse-otherOut/index.vue"),
        meta: {
            title: "其他出库"
        }
    },
    {
        path: "/warehouse-stocktaking",
        name: "warehouse-stocktaking",
        component: () => import("@/pages/wms/warehouse-stocktaking/index.vue"),
        meta: {
            title: "库存盘点"
        }
    },
    {
        path: "/warehouse-sale",
        name: "warehouse-sale",
        component: () => import("@/pages/wms/warehouse-sale/index.vue"),
        meta: {
            title: "销售出库"
        }
    },
    {
        path: "/warehouse-purchase",
        name: "warehouse-purchase",
        component: () => import("@/pages/wms/warehouse-purchase/index.vue"),
        meta: {
            title: "采购入库"
        }
    },
    /**
     * 财务
     */
    {
        path: "/income-accounts",
        name: "income-accounts",
        component: () => import("@/pages/finance/income-accounts/index.vue"),
        meta: {
            title: "应收款账单"
        }
    },
    {
        path: "/income-manager",
        name: "income-manager",
        component: () => import("@/pages/finance/income-manager/index.vue"),
        meta: {
            title: "应收款管理"
        }
    },
    {
        path: "/income-refund",
        name: "income-refund",
        component: () => import("@/pages/finance/income-refund/index.vue"),
        meta: {
            title: "应收退款单"
        }
    },
    {
        path: "/pay-accounts",
        name: "pay-accounts",
        component: () => import("@/pages/finance/pay-accounts/index.vue"),
        meta: {
            title: "应付款账单"
        }
    },
    {
        path: "/pay-manager",
        name: "pay-manager",
        component: () => import("@/pages/finance/pay-manager/index.vue"),
        meta: {
            title: "应付款管理"
        }
    },
    {
        path: "/pay-refund",
        name: "pay-refund",
        component: () => import("@/pages/finance/pay-refund/index.vue"),
        meta: {
            title: "应付退款单"
        }
    },
    {
        path: "/daily-reimburses",
        name: "daily-reimburses",
        component: () => import("@/pages/finance/daily-reimburses/index.vue"),
        meta: {
            title: "日常报销"
        }
    },
    {
        path: "/examine-reimburses",
        name: "examine-reimburses",
        component: () => import("@/pages/finance/examine-reimburses/index.vue"),
        meta: {
            title: "报销审核"
        }
    },
    {
        path: "/month-wages",
        name: "month-wages",
        component: () => import("@/pages/finance/month-wages/index.vue"),
        meta: {
            title: "月度工资"
        }
    },
    /**
     * 销售
     */
    {
        path: "/sale-order",
        name: "sale-order",
        component: () => import("@/pages/sale/sale-order/index.vue"),
        meta: {
            title: "销售订单"
        }
    },
    {
        path: "/sale-delivery",
        name: "sale-delivery",
        component: () => import("@/pages/sale/sale-delivery/index.vue"),
        meta: {
            title: "销售发货单"
        }
    },
    /**
     * 采购
     */
    {
        path: "/purchase-order",
        name: "purchase-order",
        component: () => import("@/pages/purchase/purchase-order/index.vue"),
        meta: {
            title: "采购订单"
        }
    },
    {
        path: "/purchase-arrive",
        name: "purchase-arrive",
        component: () => import("@/pages/purchase/purchase-arrive/index.vue"),
        meta: {
            title: "采购到货单"
        }
    },
    /**
     * 大模型
     */
    {
        path: "/ai-form",
        name: "ai-form",
        component: () => import("@/pages/ai/ai-form/index.vue"),
        meta: {
            title: "交流表单"
        }
    },
    {
        path: "/ai-history",
        name: "ai-history",
        component: () => import("@/pages/ai/ai-history/index.vue"),
        meta: {
            title: "交流记录"
        }
    },
    {
        path: "/blog-categories",
        name: "blog-categories",
        component: () => import("@/pages/blog/blog-categories/index.vue"),
        meta: {
            title: "博客分类"
        }
    },
    {
        path: "/blog-texts",
        name: "blog-texts",
        component: () => import("@/pages/blog/blog-texts/index.vue"),
        meta: {
            title: "博客文章"
        }
    },
    {
        path: "/blog-setting",
        name: "blog-setting",
        component: () => import("@/pages/blog/blog-setting/index.vue"),
        meta: {
            title: "博客全局配置"
        }
    }
]

export default routerList